<!--
	Desc: 	围栏表的编辑界面
	Author: caochaofeng
	Date: 	2018-08-19 17:21:27
-->

<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="120px">
      <el-form-item label="设备编码" prop="code">
        <el-input v-model="dataForm.code" placeholder="设备编码"></el-input>
      </el-form-item>
      <el-form-item label="设备名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="设备名称"></el-input>
      </el-form-item>
      <el-form-item label="锁类型" prop="catCd">
        <el-select v-model="dataForm.catCd" @change="selectHandle" placeholder="请选择锁类型">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>

      </el-form-item>
      <el-form-item label="车牌号" prop="vecNo">
        <el-col :span="24">
          <el-select clearable filterable placeholder="请选择车辆" v-model="dataForm.vecPk" @change="vecPickSelectHandle">
            <el-option v-for="item in vecNos" :key="item.vecPk" :label="item.vecNo" :value="item.vecPk"></el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="开锁密码" prop="password">
        <el-input type="password" v-model="dataForm.password" placeholder="密码"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import * as $http from '@/api/device'

  export default {
    data() {
      return {
        visible: false,
        vecNos: [],
        options: [{
          value: '0',
          label: '普通锁'
        }, {
          value: '1',
          label: '密码锁'
        }],
        dataForm: {
          id: 0,
          code: '',
          name: '',
          vecPk: '',
          catCd: '',
          catNmCn: '',
          password: '',
          vecNo: ''
        },
        dataRule: {
          catCd: [
            {required: true, message: '锁类型不能为空', trigger: 'blur'}
          ],
          code: [
            {required: true, message: '设备编码不能为空', trigger: 'blur'}
          ],
          vecNo: [
            {required: true, message: '车牌号不能为空', trigger: 'blur'}
          ],
          password: [
            {required: true, message: '开锁密码不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      selectHandle(value) {
        let _this = this;
        let obj = this.options.find((item) => {
          return item.value === value;
        });
        if (obj) {
          this.$set(this.dataForm, 'catNmCn', obj.label);         // 设置行业下拉select显示值
        } else {
          this.$set(this.dataForm, 'catNmCn', '');
        }
      },

      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.getVecs();
          if (this.dataForm.id) {
            $http.info(this.dataForm.id).then((data) => {
              if (data && data.code === 0) {
                this.dataForm = data.device
              }
            })
          }
        })
      },
      vecPickSelectHandle(value) {
        let obj = this.vecNos.find((item) => {
          return item.vecPk === value;
        });
        if (obj) {
          this.$set(this.dataForm, 'vecNo', obj.vecNo);         // 设置工程下拉select显示值
        } else {
          this.$set(this.dataForm, 'vecNo', '');
        }
      },
      //获取车辆下拉列表信息
      getVecs() {
        $http.select({}).then((data) => {
          if (data && data.code === 0) {
            this.vecNos = data.data;
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = this.dataForm
            var tick = !this.dataForm.id ? $http.add(params) : $http.update(params)
            tick.then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
